package com.suyang.syt.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.dao.interfaces.WeixinGroupDao;
import com.suyang.syt.dao.interfaces.WeixinUserDao;
import com.suyang.syt.model.User;
import com.suyang.syt.model.WeixinGroup;
import com.suyang.syt.model.WeixinUser;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinUserService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.Md5Util;
import com.suyang.syt.weixin.WeixinKit;
import com.suyang.syt.weixin.WeixinUserPager;

@Service
public class WeixinUserServiceImpl implements WeixinUserService {

	@Autowired
	private WeixinUserDao weixinUserDao;
	@Autowired
	private WeixinGroupDao weixinGroupDao;
	@Autowired
	private UserDao userDao;

	@Override
	public PagerEasyUI<WeixinUser> listByEasyUI(int page, int rows, String sort, String order) throws Exception {
		StringBuilder hql = new StringBuilder(" from WeixinUser wu where 1=1 ");
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by wu.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<WeixinUser> easyUIPager = new PagerEasyUI<WeixinUser>();
		List<WeixinUser> list = weixinUserDao.findByEasyUI(hql.toString(), page, rows);
		if (EmptyUtil.isNotEmpty(list)) {
			easyUIPager.setRows(list);
			easyUIPager.setTotal((Long) weixinUserDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		}
		return easyUIPager;
	}

	@Override
	public AjaxObj updateSync() throws Exception {
		WeixinUserPager pager = WeixinKit.listUser(null);
		if (pager != null && pager.getTotal() > 0) {
			Map<String, List<String>> map = pager.getData();
			List<String> list = map.get("openid");
			if (EmptyUtil.isNotEmpty(list)) {
				for (String openid : list) {
					if (weixinUserDao.countByOpenid(openid) <= 0) {
						weixinUserDao.add(new WeixinUser(openid));
					}
				}
			}
		}
		return new AjaxObj(1, "同步更新用户成功!");
	}

	@Override
	public AjaxObj updateBatch(String ids) throws Exception {
		List<WeixinUser> list = WeixinKit.updateBatchUser(ids);
		if (EmptyUtil.isNotEmpty(list)) {
			int groupid = -1;
			WeixinGroup weixinGroup = null;
			for (WeixinUser weixinUser : list) {
				if (groupid == -1 || groupid != weixinUser.getGroupid()) {
					weixinGroup = weixinGroupDao.loadByid(weixinUser.getGroupid());
				}
				if (weixinGroup != null) {
					weixinUser.setGroupname(weixinGroup.getName());
				}
				groupid = weixinUser.getGroupid();
				weixinUserDao.updateByOpenid(weixinUser);
			}
		}
		return new AjaxObj(1, "用户信息更新完成!");
	}

	@Override
	public AjaxObj updateRemark(String openid, String remark) throws Exception {
		if (WeixinKit.updateRemarkUser(openid, remark)) {
			weixinUserDao.updateByOpenid(openid, remark);
			return new AjaxObj(1, "用户备注信息更新完毕!");
		}
		return new AjaxObj(0, "用户备注信息更新失败,请稍后再试!");
	}

	@Override
	public User updateAuthorize(String openid) throws Exception {
		return weixinUserDao.updateAuthorize(openid);
	}

	@Override
	public AjaxObj updateBatchmoveGroup(String openid_list, int to_groupid) throws Exception {
		if (EmptyUtil.isEmpty(openid_list) || EmptyUtil.isEmpty(to_groupid)) {
			return new AjaxObj(0, "参数错误,不能移动用户!");
		}
		if (WeixinKit.updateBatchmoveGroup(openid_list, to_groupid)) {
			WeixinGroup weixinGroup = weixinGroupDao.loadByid(to_groupid);
			if (weixinGroup != null) {
				weixinUserDao.updateBatchmoveGroup(openid_list, weixinGroup.getId(), weixinGroup.getName());
				return new AjaxObj(1, "用户移动成功!");
			}
		}
		return new AjaxObj(0, "用户移动失败!");
	}

	@Override
	public AjaxObj updateBinding(User user, String phone, String passwd, String passwdRe) throws Exception {
		if (user == null) {
			return new AjaxObj(0, "用户信息读取失败!");
		}
		if (EmptyUtil.isEmpty(phone)) {
			return new AjaxObj(0, "用户绑定过程,手机号码不能为空!");
		}
		if (EmptyUtil.isEmpty(passwd)) {
			return new AjaxObj(0, "用户绑定过过程,密码不能为空!");
		}
		if (!passwd.equals(passwdRe)) {
			return new AjaxObj(0, "用户绑定过过程,密码两次输入不一致!");
		}
		if (userDao.countUsername(phone) > 0 || userDao.countPhone(phone) > 0) {
			return new AjaxObj(0, "该手机已经被注册过!");
		}
		user.setPhone(phone);
		user.setUsername(phone);
		user.setPassword(Md5Util.md5(phone, passwd));
		user.setStatus(1);
		user.setModifyName("微信绑定");
		user.setModifyTime(DateFormat.dateToString(new Date()));
		userDao.update(user);
		WeixinUser weixinUser = weixinUserDao.loadByUid(user.getId());
		if (EmptyUtil.isEmpty(weixinUser)) {
			return new AjaxObj(0, "微信用户加载失败,请联系管理员!");
		}
		weixinUser.setStatus(1);
		weixinUserDao.update(weixinUser);
		return new AjaxObj(1, "用户绑定成功 !");
	}

	@Override
	public AjaxObj updatePwd(User user, String passwdNew, String passwdRe) throws Exception {
		if (user == null || EmptyUtil.isEmpty(user.getUsername())) {
			return new AjaxObj(0, "获取用户信息失败，请稍后再试！");
		}
		if (passwdNew == null || "".equals(passwdNew.trim())) {
			return new AjaxObj(0, "新密码不能为空！");
		}
		if (!passwdNew.equals(passwdRe)) {
			return new AjaxObj(0, "两次密码不一致");
		}
		user.setPassword(Md5Util.md5(user.getUsername(), passwdNew));
		userDao.update(user);
		return new AjaxObj(1, "密码更新成功，请牢记新密码！", passwdNew);
	}

}
